from rest_framework import permissions
from django.conf import settings


class IsAppEngineScheduler(permissions.BasePermission):
    def has_permission(self, request, view):
        is_schedule = request.META.get('HTTP_X_CLOUDSCHEDULER') == 'true'
        is_valid_host = check_host(request.META)
        return is_valid_host and is_schedule


class IsGcloudCronJob(permissions.BasePermission):
    def has_permission(self, request, view):
        is_schedule = request.META.get('HTTP_X_NGINX_CRON') == 'true'
        is_valid_host = check_host(request.META)
        return is_valid_host and is_schedule


class IsGCloudTask(permissions.BasePermission):
    def has_permission(self, request, view):
        queue_name = request.META.get('HTTP_X_APPENGINE_QUEUENAME')
        is_valid_queue = queue_name == settings.MAIN_QUEUE
        is_valid_host = check_host(request.META)
        return is_valid_host and is_valid_queue


# def check_host(meta):
#     host_name = meta.get("HTTP_HOST")
#     return host_name == settings.APPENGINE_DEFAULT_VERSION_HOSTNAME

def check_host(meta):
    host_name = meta.get("HTTP_HOST")
    return host_name == "localhost:8000"

